package com.example.myapplication.Adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.Bean.Notepad;
import com.example.myapplication.MainActivity;
import com.example.myapplication.databinding.ItemListBinding;
import com.example.myapplication.nodework;
import com.example.myapplication.ui.EditActivity;

import java.util.ArrayList;
import java.util.List;


public class NodepadListAdapter extends RecyclerView.Adapter<NodepadListAdapter.VH> {

    List<Notepad> list = new ArrayList<>();
    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemListBinding binding = ItemListBinding.inflate(layoutInflater);
        return new VH(binding);
    }
    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(@NonNull VH holder,  int position) {
        Notepad notepad = list.get(position);
        holder.binding.contentTv.setText(notepad.getContent());
        holder.binding.timeTv.setText(notepad.getTime());

        //长按删除事件
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                showDelteDialog(notepad,v.getContext());
//                Notepad currentNotepad = list.get(position);
                return false;
            }
        });

        //点击编辑内容
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nodework activity =(nodework) v.getContext();
                Intent intent = new Intent(activity, EditActivity.class);
                intent.putExtra("nodepad",notepad);
                activity.startActivityForResult(intent,100);
            }
        });

    }
    //长按模态框
    private void showDelteDialog(Notepad notepad, Context context) {
        AlertDialog alertDialog = new AlertDialog.Builder(context)
                .setTitle("温馨提示")
                .setMessage("确定删除这一项吗")
                .setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        nodework node = (nodework)context;
                        node.myDbHelper.delete(notepad);
                        //notepad.getContent() 可以打印出当前行的信息
//                        Log.d("del",notepad.toString());
                        node.findAll();
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "点击了取消", Toast.LENGTH_SHORT).show();
                    }
                })
                .create();
        alertDialog.show();

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void update(List<Notepad> notepadList) {
        this.list.clear();
        this.list.addAll(notepadList);
        //刷新一下界面
        notifyDataSetChanged();
    }

    class VH extends RecyclerView.ViewHolder{
        ItemListBinding binding;
        public VH(@NonNull ItemListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
